package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list=roleService.findAll();

            return new Result(true,"查询角色列表成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色列表失败");
        }
    }

    @RequestMapping("/add")
    public Result add(Integer userId,@RequestBody Integer[]roleIds){
        try {
            roleService.add(userId,roleIds);
            return new Result(true,"添加用户角色权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户角色权限失败");
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.pageQuery(queryPageBean);
    }

    @RequestMapping("/editPermission")
    public Result editPermission(Integer roleId,@RequestBody Integer[]permissionIds){
        try {
            roleService.editPermission(roleId,permissionIds);
            return new Result(true,"添加用户角色权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户角色权限失败");
        }
    }

    //addRole

    @RequestMapping("/addRole")
    public Result addRole(@RequestBody Role role){
        try {
            roleService.addRole(role);
            return new Result(true,"添加角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加角色失败");
        }
    }
}
